/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.xstgongyi.eduaid.dao.AccountingSubjectMapper;
import org.xstgongyi.eduaid.dataobject.AccountingSubjectDO;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author oldcrane
 * @created 2019-04-22
 */
@Service
@CacheConfig(cacheNames = "accounting-subject")
public class AccountingSubjectManager {

    @Autowired
    private AccountingSubjectMapper subjectDao;

    @Cacheable(key = "#id")
    public AccountingSubjectDO getById(Integer id) {
        return subjectDao.getById(id);
    }

    @Cacheable(key = "'list-all'")
    public List<AccountingSubjectDO> listAll() {
        return subjectDao.list(null);
    }

    @CacheEvict(allEntries = true)
    public void insert(AccountingSubjectDO subject) {
        LocalDateTime now = LocalDateTime.now();
        subject.setGmtCreated(now);
        subject.setGmtUpdated(now);
        subjectDao.insert(subject);
    }

    @CacheEvict(allEntries = true)
    public void update(AccountingSubjectDO subject) {
        subject.setGmtUpdated(LocalDateTime.now());
        subjectDao.update(subject);
    }

}
